/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("equipment",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VolPro.Entity.DomainModels;
using SMes.DbMethod.IServices;
using SMes.DbMethod.IRepositories;
using System.Linq;
using VolPro.Core.Extensions;
using Microsoft.EntityFrameworkCore;

namespace SMes.DbMethod.Controllers
{
    public partial class equipmentController
    {
        private readonly IequipmentService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IequipmentRepository _repository;

        [ActivatorUtilitiesConstructor]
        public equipmentController(
            IequipmentService service,
            IHttpContextAccessor httpContextAccessor,
            IequipmentRepository repository
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _repository = repository;
    }
        [Route("search"), HttpPost]
        public async Task<ActionResult> Search([FromBody] PageDataOptions loadData)
        {
            string value = loadData.Value?.ToString()?.Trim();
            var query = _repository.WhereIF(!string.IsNullOrEmpty(value), x => x.SerialNo.Contains(value) || x.EquipmentName.Contains(value));
            var data = new
            {
                rows = await query.OrderByDescending(r => r.SerialNo)
                .TakePage(loadData.Page, loadData.Rows)
                .Select(d => new
                {
                    d.EquipmentID,
                    d.SerialNo,
                    d.EquipmentName,
                    d.Status,
                    d.Memo
                }).ToListAsync(),
                total = await query.CountAsync()
            };

            return JsonNormal(data);
        }
    }
}
